﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using huqiang.Framework;

namespace huqiang.Communication
{
    public class KcpManager:HFBase
    {
        protected override string TypeName => "KcpManager";
        public KcpServer server { get; private set; }
        /// <summary>
        /// 服务器端口
        /// </summary>
        public int Port = 0;
        /// <summary>
        /// 用户连接缓存数
        /// </summary>
        public int UserBufferCount = 2048;
        public void SetClientFactory(Func<KcpLink> func)
        {
            server.CreateNew = func;
        }
        public void SetClientFactory<T>() where T : KcpLink, new()
        {
            server.CreateNew = () => { return new T(); };
        }
        protected override void Initial()
        {
            server = new KcpServer();
            server.CreateNew = () => { return new KcpUser(); };
            server.OpenHeart();
        }
        protected override void Start()
        {
            server.Run(Port, UserBufferCount);
        }
        public override void Dispose()
        {
            if(server!=null)
            {
                server.CloseHeart();
                server.Dispose();
                server = null;
            }
        }
    }
}
